#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<cstdlib>//rand()
#define rep(i,a,b) for(register int i = (a);i <= (b);++i)
#define per(i,a,b) for(register int i = (a);i >= (b);--i)
typedef long long ll;
typedef unsigned long long ull;
using std::string;using std::cin;using std::cout;

int n,m,w[305];
int head[605],next[605],ver[605],tot;
int dp[305][305];//i节点，一共选j个

inline void link(int x,int y){ver[++tot] = y , next[tot] = head[x] , head[x] = tot;}

inline void dfs(int now){
    for(int i = head[now];i;i = next[i]){
        dfs(ver[i]);
        per(j,m+1,1){//因为0不能算进去，所以特别的给个m+1，实际上可以只给now=0时这个待遇
            rep(k,1,j-1){//后面选k个
                dp[now][j] = std::max(dp[now][j],dp[now][j-k] + dp[ ver[i] ][k]);
            }
        }
    }
    return;
}

int main(){
    std::ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //freopen("in.in", "r", stdin);
    cin >> n >> m;
    rep(u,1,n){
        int v;
        cin >> v >> w[u];
        link(v,u);
    }
    rep(i,1,n) dp[i][1] = w[i];
    dfs(0);
    
    // rep(i,0,n) rep(j,1,m) cout << i << " " << j << " " << dp[i][j] << "\n";
    
    cout << dp[0][m+1] << "\n";
    return 0;
}